package guda.stark.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.stark.biz.UnitBiz;
import guda.stark.dal.dao.UnitDAO;
import guda.stark.dal.dataobject.UnitDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class UnitBizImpl implements UnitBiz{

    private final static Logger logger = LoggerFactory.getLogger(UnitBizImpl.class);

    @Autowired
    private UnitDAO unitDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            UnitDO unitDO = unitDAO.getUnitById(id);
            bizResult.data.put("unitDO", unitDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Unit error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = unitDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<UnitDO> unitList = unitDAO.selectForPage(baseQuery);
            bizResult.data.put("unitList", unitList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Unit list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            unitDAO.delUnitById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete unit error", e);
        }
        return bizResult;
    }

    public BizResult create(UnitDO unitDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = unitDAO.insert(unitDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Unit error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(UnitDO unitDO) {
        BizResult bizResult = new BizResult();
        try {
            unitDAO.updateByIdSelective(unitDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Unit error", e);
        }
        return bizResult;
    }


}
